package com.gxwzxy.medic.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.gxwzxy.medic.dao.InstitutionDao;
import com.gxwzxy.medic.entity.Institution;
import com.gxwzxy.medic.until.Connect;

public class InstitutionDaoImpl implements InstitutionDao {

	//声明连接数据库的变量
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement ps = null;
	
	/**
	 * 查询农合经办点个数
	 */
	@Override
	public int findInSize() {
		String sql = "select COUNT(*) from institution";
		int i = 0;
		try {
			//调用静态方法
			rs = Connect.executeF(sql);
			if(rs.next()) {
				i = rs.getInt(1);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return i;
	}

	/**
	 * 分页查询，start是开始的页数，size是一页行有多少数据
	 */
	@Override
	public List<Institution> findInsIndex(int start, int size) {
		// 声明变量
		ArrayList<Institution> ai = new ArrayList<>();
		String sql = null;
		try {
			// 创建Sql语句
			sql = "select * from institution limit ?,?";
			// 执行sql语句
			rs =Connect.executeF(sql, start,size);
			// 获取结果
			while (rs.next()) {
				Institution a = new Institution(rs.getString(1), rs.getString(2), rs.getString(3),rs.getString(4));
				ai.add(a);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Connect.close(conn, ps, rs);
		}
		// 关闭连接
		return ai;
	}

	/**
	 * 查询所有经办点
	 */
	@Override
	public List<Institution> findAllIns() {
		// 声明变量
		ArrayList<Institution> ai = new ArrayList<>();
		String sql = null;
		try {
			// 创建Sql语句
			sql = "select * from institution ";
			// 执行sql语句
			rs =Connect.executeF(sql);
			// 获取结果
			while (rs.next()) {
				Institution a = new Institution(rs.getString(1), rs.getString(2), rs.getString(3),rs.getString(4));
				ai.add(a);
			}
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			Connect.close(conn, ps, rs);
		}
		return ai;
	}

	/**
	 * 添加新经办机构
	 */
	@Override
	public boolean saveIns(String agenName, String areaCode, String areagrade) {
		//创建SQL语句
		String sql = "insert into institution(areacode,agencode,agenname,grade) value(?,?,?,?)";
		String agenCode = areaCode+"1";//经办机构编号=行政区域编号+1
		int i = Connect.executeDML(sql, areaCode,agenCode,agenName,areagrade);
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 根据agenCode修改经办机构
	 */
	@Override
	public boolean updateIns(String agenCode, String agenName) {
		//创建SQL语句
		String sql = "update institution set agenname = ? where agencode = ?";
		int i = Connect.executeDML(sql, agenName,agenCode);
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 根据agenCode删除经办机构
	 */
	@Override
	public boolean deleteIns(String agenCode) {
		//创建SQL语句
		String sql = "delete from institution where agencode = ?";
		int i = Connect.executeDML(sql,agenCode);
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

}
